package com.qf.controller;

import com.qf.pojo.Sys_user;
import com.qf.service.Sys_userService;
import com.qf.util.JsonUtils;
import com.qf.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
/**
 * 定义访问接口
 */
@RequestMapping("/userController")
public class Sys_userController {
      @Autowired
      Sys_userService sys_userService;


    /**
     * 注册
     * @param sys_user
     * @return
     */
    @RequestMapping(value="/reg",method = RequestMethod.GET)
    @ResponseBody

    public ActionResult registerfor(Sys_user sys_user){

        System.out.println("注册获取页面数据:"+sys_user);

        boolean flag = sys_userService.registerfor(sys_user);
        System.out.println("是否获取到后台数据:"+flag);
        if(flag){

            ActionResult actionResult=new ActionResult(200,"登录成功",flag);
            return actionResult;
        }

        return null;


    }


    /**
     *  登录
     * @param sys_user
     * @return
     */
    @RequestMapping(value="/log",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult logining(Sys_user sys_user, HttpSession session) {


        System.out.println("获取到页面数据" + sys_user);
        Sys_user user = sys_userService.loginfor(sys_user);
        System.out.println("获取到方法数据:" + user);
        if(user!=null){
            session.setAttribute("user",user);
            ActionResult actionResult=new ActionResult(200,"登录成功",user);
            return actionResult;
        }
        return null;
    }


    /**
     * 管理员查询全部用户显示
     * @return
     */
    @RequestMapping(value = "/getCarUser",method = RequestMethod.GET)
    @ResponseBody
    public List<Sys_user> getUserInfo(){

        List<Sys_user> users = sys_userService.findCarUser();


        return users;
    }


    /**
     * 管理员删除用户
     * @param id
     * @return
     */
    @RequestMapping(value = "/delUser",method = RequestMethod.DELETE)
    @ResponseBody
    public ActionResult delByid(int id){
        boolean bool= sys_userService.deleteUserById(id);
        return new ActionResult(200,"删除成功",null);
    }


    /**
     * 查询用户
     * @param id
     * @return
     */
    @RequestMapping(value = "getUserInfoById/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult getUserInfoByid(@PathVariable("id") int id){

        Sys_user user=sys_userService.findById(id);

        return new ActionResult(200,null,user);
    }


    @RequestMapping(value = "/modify",method = RequestMethod.PUT)
    @ResponseBody
    public ActionResult modify(@RequestBody Sys_user sys_user){
        System.out.println(sys_user);
        sys_userService.Userupdate(sys_user);
        System.out.println("修改完成");
        return new ActionResult(200,"修改成功",null);
    }



    @RequestMapping("/denglu")
    @ResponseBody
    public ActionResult findUser(HttpSession session) {
        ActionResult bean = null;

        Sys_user user1 = (Sys_user) session.getAttribute("user");
        if (user1 == null) {
            bean = JsonUtils.createJsonBean(0,0);
        } else {
            bean = JsonUtils.createJsonBean(1,user1);
        }

        return bean;

    }


    /**
     * 个人信息修改
     * @param session
     * @param tel
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult updateTel(HttpSession session, String tel) {
        ActionResult bean = null;
        Sys_user user1 = (Sys_user) session.getAttribute("user");
        System.out.println(user1);
        user1.setTel(tel);
        try {
            System.out.println("修改用户");
            sys_userService.update(user1);
            System.out.println(user1);
            System.out.println("修改用户信息完成");
            bean = JsonUtils.createJsonBean(1,"修改成功！");
        } catch (Exception e) {
            bean = JsonUtils.createJsonBean(0,e.getMessage());
        }


        return bean;

    }




    /**
     * 查询单个用户
     * @return
     */
    @RequestMapping(value = "/getIduser",method = RequestMethod.GET)
    @ResponseBody
    public ActionResult getIduser(HttpSession session){
        System.out.println("seession:"+session);
        Sys_user userone = (Sys_user) session.getAttribute("user");
        System.out.println(userone);
        Sys_user user=sys_userService.findById(userone.getId());
        System.out.println("执行查找后的:"+user);
        return new ActionResult(200,null,user);
    }



}
